package com.eddie.stack;

//定义一个arrayStack表示栈结构
class ArrayStack2 {
    private int maxSize;//栈的大小
    private int[] stack;//数组模拟栈
    private int top = -1;//top表示栈顶

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(int num) {
        if (!isFull()) {
            top++;
            stack[top] = num;
        } else {
            System.out.println("栈已满");
        }
    }

    //返回栈顶数据，但是不是pop（仍然保留）
    public int peek() {
        return stack[top];
    }

    public int pop() {
        if (isEmpty()) {
            System.out.println("栈为空");
        } else {
            return stack[top--];
        }
        return -1;
    }

    //遍历栈
    public void show() {
        if (!isEmpty()) {
            for (int i = top; i >= 0; i--) {
                System.out.println(stack[i]);
            }
        }
    }

    //返回运算符的优先级，由程序员来决定
    //数字越大优先级越高
    public int priority(int opr) {
        if (opr == '*' || opr == '/') {
            return 1;
        } else if (opr == '+' || opr == '-') {
            return 0;
        }
        return -1;
    }

    public boolean isOpr(char val) {
        return priority(val) != -1;
    }

    public int cal(int num1, int num2, int opr) {
        int res = 0;//存放计算的结果
        switch (opr) {
            case '+' -> {
                res = num2 + num1;
            }
            case '-' -> {
                res = num2 - num1;
            }
            case '*' -> {
                res = num2 * num1;
            }
            case '/' -> {
                res = num2 / num1;
            }
        }
        return res;
    }


}
